
import {
    getStatementsList,
    doApproval,
    pay,
    getStatementById,
    getPayById,
    getApprovalById,
    updateStatus,
    exportStatement,
    exportStatementList
} from '../../services/capital/approval';
import Constants from '../../utils/Constants';
import {parse} from 'qs';
import { message } from 'antd';
import { getCurrentNodeDetail} from '../../services/customer/businessagreement';

const R = require('ramda');

export default {
    namespace: 'capital',
    state: {
        page: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },
        approvalPage: {
            page: 1,
            size: 10,
            totalRow: 0,
            totalPage: 0
        },

        list:[],
        approvalList:[],
        payList:[],

        dataSource: {},

        capital: {},
        node:{},
        status: "",
    },
    reducers: {

        //-----------------setList

        /**
         *
         * @param state
         * @param payload
         * @returns {{list, page: *}}
         */
        setlist(state, { payload }) {
            return { ...state, list: payload.list, page: R.isNil(payload.page) ? state.page : payload.page, status : payload.status}
        },


        /**
         * 设置审核记录
         * @param state
         * @param action
         * @returns {{approvaList}}
         */
        setApprovalList(state, { payload }) {
            return { ...state, approvalList: payload.list, approvalPage: R.isNil(payload.page) ? state.approvalPage : payload.page}
        },

        /**
         *
         * 设置付款列表
         * @param state
         * @param payload
         * @returns {{pay}}
         */
        setPayList(state, { payload }) {
            return { ...state, payList: payload.list}
        },


        //--------------------setEntity


        /**
         * 设置结算单
         * @param state
         * @param action
         * @returns {{dataSource: (string|state.dataSource|{}|*)}}
         */
        setDataSource(state, action) {
            let payload = action.payload;
            return {...state, dataSource: payload.data};
        },

        /**
         * 工作流审核节点信息
         * @param state
         * @param action
         * @returns {{node}}
         */
        setNode(state, action) {
            let payload = action.payload;
            return {...state, node: payload.node,};
        },



    },
    effects: {

        /**
         * 分页查询结算单列表
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getStatementsList({payload}, { call, put, select }) {
            let page = yield select(state => state.capital.page);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            let status = yield select(state => state.capital.status);
            status = R.isNil(payload) || R.isNil(payload.status) ? status : payload.status;

            const res = yield call(getStatementsList, {
                status: R.isNil(payload) || R.isNil(payload.status) ? "" : payload.status,
                stateno: R.isNil(payload) || R.isNil(payload.stateno) ? "" : payload.stateno,
                customerno: R.isNil(payload) || R.isNil(payload.customerno) ? "" : payload.customerno,
                beginTime: R.isNil(payload) || R.isNil(payload.beginTime) ? "" : payload.beginTime,
                endTime: R.isNil(payload) || R.isNil(payload.endTime) ? "" : payload.endTime,
                minamount: R.isNil(payload) || R.isNil(payload.minamount) ? "" : payload.minamount,
                maxamount: R.isNil(payload) || R.isNil(payload.maxamount) ? "" : payload.maxamount,
                creator: R.isNil(payload) || R.isNil(payload.creator) ? "" : payload.creator,
                page
            });

            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setlist',
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                        status: status
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },



        /**
         * 结算单详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getStatementById({ payload }, { call, put, select }) {
            const res = yield call(getStatementById, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setDataSource',
                    payload: {
                        data : content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 付款详情
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getPayById({ payload }, { call, put, select }) {
            const res = yield call(getPayById, payload);
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: 'setPayList',
                    payload: {
                        list : content
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 付款
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *doPay({payload}, {call, put, select}) {
            const page = yield select(state => state.capital.page);
            let status = yield select(state => state.capital.status);

            const res = yield call(pay, payload);

            if (res.data.resultCode === "ok") {
                message.info("审核成功！");
                let content = res.data.content;
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 付款
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *updateStatus({payload}, {call, put, select}) {

            const res = yield call(updateStatus, payload);

            if (res.data.resultCode === "ok") {
                message.info("提交审核！");
                let content = res.data.content;
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },



        /**
         * 审核
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *doApproval({ payload }, { call, put, select }) {
            const page = yield select(state => state.capital.page);
            let status = yield select(state => state.capital.status);

            const res = yield call(doApproval, payload);
            if (res.data.resultCode === "ok") {
                message.info("审核成功！");
                if(payload.onSuccess)payload.onSuccess();

            } else {
                message.error(res.data.errMessage);
            }
        },


        /**
         * 获取审核记录
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getApprovalById({ payload }, { call, put, select }) {
            let page = yield select(state => state.capital.approvalPage);
            page = R.isNil(payload) || R.isNil(payload.page) ? page : payload.page;

            const res = yield call(getApprovalById, { id: payload.id, page });
            if (res.data.resultCode === "ok") {
                let content = res.data.content;
                yield put({
                    type: "setApprovalList",
                    payload: {
                        list: R.isNil(content.list) ? [] : content.list,
                        page : R.isNil(content.page) ? Constants.DEFAULTPAGE : content.page,
                    }
                });
            } else {
                message.error(res.data.errMessage);
            }

        },

        /**
         * 获取当前工作流审核节点
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *getCurrentNodeDetail({ payload }, { call, put, select }) {
            const res = yield call(getCurrentNodeDetail, {id: payload.id, code: payload.code});

            if (res.data.resultCode === "ok") {
                let data = res.data.content;
                yield put({
                    type: "setNode",
                    payload: {
                        node: data,
                    }
                });

            }
        },


        /**
         * 导出申请书
         * @param payload
         * @param call
         * @param put
         * @param select
         */
        *exportStatement({ payload }, { call, put, select }) {
            const res = yield call(exportStatement, {id: payload.id});
        },


        /**
         *  导出结算单列表
         * @param payload
         * @param call
         * @param put
         * @param select
         * @returns {IterableIterator<*>}
         */
        *exportStatementList({ payload }, { call, put, select }) {
            const res = yield call(exportStatementList, {
                status: payload.status,
                stateno: payload.stateno,
                customerno: payload.customerno,
                minamount: payload.minamount,
                maxamount: payload.maxamount,
                beginTime: payload.beginTime,
                endTime: payload.endTime,
            });
        },

    },
    subscriptions: {
        setup({ history, dispatch }) {

        }
    }
};